<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="form.mobile" />
      </el-form-item>
      <el-form-item label="身份证号" prop="identity_number">
        <el-input v-model="form.identity_number" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" />
      </el-form-item>
      <el-form-item label="角色" prop="role_id">
        <el-select v-model="form.role_id" placeholder="请选择角色" filterable>
          <el-option
            v-if="type === 'edit' && !form.role_id"
            :value="0"
            label="超级管理员"
          />
          <el-option
            v-for="role in roles"
            :key="role.id"
            :value="role.id"
            :label="role.role_name"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doAdd, doEdit } from '@/api/manager'
  import { getList as getRoleList } from '@/api/role'
  import { isName, isPhone, isIdCard, isPassword } from '@/utils/validate'

  export default {
    name: 'ManagerEdit',
    data() {
      return {
        form: {
          name: '',
          mobile: '',
          identity_number:'',
          password: '',
          role_id: '',
        },
        roles: [],
        title: '',
        dialogFormVisible: false,
        type: null,
      }
    },
    computed: {
      rules() {
        const rules = {
          name: [
            { required: true, trigger: 'blur', message: '请输入姓名' },
            { validator: this.checkName, trigger: 'blur' },
          ],
          mobile: [
            { required: true, trigger: 'blur', message: '请输入手机号' },
            { validator: this.checkMobile, trigger: 'blur' },
          ],
          identity_number: [
            { required: true, trigger: 'blur', message: '请输入身份证号码' },
            { validator: this.checkIdCard, trigger: 'blur' },
          ],
          password: [
            { validator: this.checkPassword, trigger: 'blur' },
            { required: true, trigger: 'blur', message: '请输入密码' },
          ],
          role_id: [
            { required: true, trigger: 'change', message: '请选择角色' },
          ],
        }
        return rules
      },
    },
    watch: {
      dialogFormVisible(visible) {
        if (visible) this.fetchRoles()
      },
    },
    created() {
      this.fetchRoles()
    },
    methods: {
      async fetchRoles() {
        const { data } = await getRoleList()
        this.roles = data
      },
      showEdit(row) {
        if (!row) {
          this.type = 'add'
          this.title = '添加'
        } else {
          this.type = 'edit'
          this.title = '编辑'
          this.form = {
            ...row,
            manager_id: row.id,
          }
        }
        this.dialogFormVisible = true
      },
      checkName(rule, value, callback) {
        if (!isName(value))
          callback(
            new Error('姓名只允许中文、英文、数字，不能包含空格、特殊符号'),
          )
        else callback()
      },
      checkMobile(rule, value, callback) {
        if (!isPhone(value)) callback(new Error('手机号格式错误'))
        else callback()
      },
      checkIdCard(rule, value, callback) {
        if (!isIdCard(value)) callback(new Error('身份证号码格式错误'))
        else callback()
      },
      checkPassword(rule, value, callback) {
        if (!value) callback()
        else if (!isPassword(value)) callback(new Error('密码至少6位'))
        else callback()
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            let msg
            if (this.type === 'add') msg = await doAdd(this.form)
            else msg = await doEdit(this.form)
            this.$baseMessage(msg, 'success')
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
